package com.restaurant.application.genshanguser;

import com.google.common.base.Strings;
import com.nebutown.base.ViewSpec;
import com.nebutown.base.data.View;
import com.nebutown.base.data.ViewUpdate;
import com.nebutown.commonunits.dataface.DataModes;
import com.nebutown.commonunits.dataface.EntryTypes;
import com.nebutown.commonunits.dataface.FormShell;
import com.nebutown.commonunits.dataface.definitions.PageForm;
import com.nebutown.commonunits.uidataface.UIDataFace;
import com.nebutown.commonunits.utils.DateTimeUtils;
import com.nebutown.commonunits.utils.SnowFlake;
import com.nebutown.generation.basicdata.StringList;
import com.nebutown.generation.basictype.UID;
import com.restaurant.application.genshanguser.support.AbstractGenShangUser;
import com.restaurant.application.genshanguser.support.GenShangUserData;
import com.restaurant.application.indexes.genshanguserList.AssistGenShangUserList;
import com.restaurant.application.indexes.genshanguserList.mysql.GenShangUserListBean;
import com.restaurant.application.indexes.genshanguserList.mysql.GenShangUserListCriteria;
import com.restaurant.application.indexes.genshanguserList.mysql.GenShangUserListImpl;

import java.util.List;
import java.util.Objects;

/**
 * @author Administrator
 */
public class GenShangUserImpl extends AbstractGenShangUser {
    @Override
    public ViewUpdate save(GenShangUserData data, int[] seq) {
        String msg = getStr(data);
        if (!Strings.isNullOrEmpty(msg)) {
            return returnViewCall(buildViewCall(unitPosition(), "showErrorMessage", null, new StringList().addData(msg)));
        }

        perfect(data);

        if (Objects.isNull(getData().getCreateTime())) {
//            todo number 没有添加
            getData()
                    .withId(SnowFlake.findUniqueId())
                    .withCreateTime(DateTimeUtils.getCurrentMillisecond());

//            写入大表单
            GenShangUserListBean bean = getSingleton(AssistGenShangUserList.class).createBean(getId(), getData());
            getIndex(GenShangUserListImpl.class).insert(bean);
        }
        return returnPopFromContainer(getId(), false);
    }

    @Override
    public ViewUpdate cancel(int[] seq) {
        return returnPopFromContainer(getId(), false);
    }

    private String getStr(GenShangUserData data) {
        if (Objects.isNull(getData().getCreateTime())) {
            if (Strings.isNullOrEmpty(data.getNickName())) {
                return "请填写用户昵称";
            }
            if (Strings.isNullOrEmpty(data.getPassword())) {
                return "请填写密码";
            }
        }
//        查重
        if (!Strings.isNullOrEmpty(data.getNickName())) {
            List<GenShangUserListBean> beans = getIndex(GenShangUserListImpl.class)
                    .search(new GenShangUserListCriteria().withNickNameEquals(data.getNickName()).withUserUidNotEquals(getId()), null, 1);
            if (beans.size() > 0) {
                return "该昵称已使用请重新填写";
            }
        }

        return null;
    }

    @Override
    public View getFullView() {
        return newCommonTable(DataModes.EDIT);
    }

    @Override
    public View getViewLead(ViewSpec spec) {
        return null;
    }

    private void perfect(GenShangUserData data) {
        getData().withModifyTime(DateTimeUtils.getCurrentMillisecond());
        if (!Strings.isNullOrEmpty(data.getAvatarUrl())) {
            getData().withAvatarUrl(data.getAvatarUrl());
        }
        if (!Strings.isNullOrEmpty(data.getRealName())) {
            getData().withRealName(data.getRealName());
        }
        if (!Strings.isNullOrEmpty(data.getNickName())) {
            getData().withNickName(data.getNickName());
        }
        if (!Strings.isNullOrEmpty(data.getPassword())) {
            getData().withPassword(EncryptSHA.sha256(data.getPassword()));
        }
        if (!Strings.isNullOrEmpty(data.getPhone())) {
            getData().withPhone(data.getPhone());
        }
        if (!Strings.isNullOrEmpty(data.getGender())) {
            getData().withGender(data.getGender());
        }
        if (!Strings.isNullOrEmpty(data.getState())) {
            getData().withState(data.getState());
        }
    }

    private View newCommonTable(DataModes dataModes) {
        PageForm pageForm = newFormShell(dataModes).getForm();
        return getSingleton(UIDataFace.class).newGenericForm(unitPosition(), pageForm);
    }

    public FormShell newFormShell(DataModes dataModes) {
        FormShell shell = new FormShell("用户管理/新建用户", 6, 1);

        GenShangUserData data = getData();

        boolean editable = false;
        if (DataModes.EDIT.equals(dataModes)) {
            editable = true;
        }
        shell.source(data, dataModes);

        shell.at(0, 0, "昵称")
                .getEntry(GenShangUserData.FN_NICKNAME, data.getNickName())
                .placeHolder("昵称")
                .notNull()
                .simpleEntry(EntryTypes.STRING)
                .getContent().setEditable(editable);
        shell.at(1, 0, "密码")
                .getEntry(GenShangUserData.FN_PASSWORD, data.getPassword())
                .placeHolder("密码")
                .simpleEntry(EntryTypes.STRING)
                .notNull()
                .getContent().setEditable(editable);
        shell.at(2, 0, "姓名")
                .getEntry(GenShangUserData.FN_REALNAME, data.getRealName())
                .placeHolder("姓名")
                .simpleEntry(EntryTypes.STRING)
                .getContent().setEditable(editable);
        shell.at(3, 0, "性别")
                .getEntry(GenShangUserData.FN_GENDER, data.getGender())
                .placeHolder("性别")
                .selectFrom("男", "女", "未知")
                .getContent().setEditable(editable);
        shell.at(4, 0, "手机")
                .getEntry(GenShangUserData.FN_PHONE, data.getPhone())
                .placeHolder("手机")
                .simpleEntry(EntryTypes.STRING)
                .lengthLimit(11)
                .getContent().setEditable(editable);
        shell.at(5, 0, "状态")
                .getEntry(GenShangUserData.FN_STATE, data.getState())
                .placeHolder("状态")
                .selectFrom(STATE_FROZEN, STATE_NORMAL)
                .getContent().setEditable(editable);

        shell.at(6, 0, "取消").buttonField(getPath(CMD_CANCEL), "", true);
        shell.at(6, 1, "提交").buttonField(getPath(CMD_SAVE), "", true);
        return shell;
    }

    @Override
    public void asFirstUser() {
        GenShangUserData data = new GenShangUserData()
                .withRealName("admin")
                .withNickName("admin")
                .withPassword("admin")
                .withState(STATE_NORMAL);
        save(data, null);
    }

    @Override
    public GenShangUserData findData() {
        return getData().duplicate();
    }

    @Override
    public UID getDesktop(boolean tall) {
        return UserDesktop.buildWide(this);
    }
}
